package com.htdata.htiiot.main;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.htdata.htiiot.bean.EventToRedis;
import com.htdata.htiiot.mysql.DbConnectionPool;
import com.htdata.htiiot.redis.ConRedis;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

/**
 * 该类用于将mysql中的event阈值存入redis中
 * redis中的存储格式key:event_[thing_device_component_rel表字段id]_[thing_data_stream表字段name]
 *               value:id为event表字段id
 */
public class ThresholdFromMysqlToRedis {
    private static Logger log = Logger.getLogger(ThresholdFromMysqlToRedis.class);
    private static DbConnectionPool dbPool = DbConnectionPool.getPool();
    public static void main(String[] args) {
        Connection conn = null;
        Jedis jedis = null;
        PreparedStatement prep = null;
        try {
            conn = dbPool.getConnection();
            prep = conn.prepareStatement("select rel.id as relId,stream.name,event.id,event.operator,event.threshold_min,event.threshold_max \n" +
                    "FROM\n" +
                    "thing_event event,thing_data_stream stream,thing_device_component_rel rel \n" +
                    "where \n" +
                    "event.stream_id=stream.id and stream.component_id=rel.component_id and event.deleted=false");
            ResultSet r = prep.executeQuery();
            Map<String,List<EventToRedis>> map = new HashMap<>();
            while (r.next()){
                String key = "event_"+r.getString(1)+"_"+r.getString(2);
                List<EventToRedis> l = map.getOrDefault(key,new ArrayList<EventToRedis>());
                EventToRedis e = new EventToRedis();
                e.setId(r.getInt(3));
                e.setOperator(r.getString(4));
                e.setThresholdMin(r.getString(5));
                e.setThresholdMax(r.getString(6));
                l.add(e);
                map.put(key,l);
            }
            prep.close();
            conn.close();
            Iterator<String> it = map.keySet().iterator();
            ConRedis  redis = new ConRedis();
            jedis = ConRedis.getConn();
            while (it.hasNext()){
                String key = it.next();
                List<EventToRedis> l = map.get(key);
                ObjectMapper objectMapper = new ObjectMapper();
                ArrayNode arrayNode = objectMapper.createArrayNode();
                for(EventToRedis e:l){
                    ObjectNode node = objectMapper.createObjectNode();
                    node.put("id",e.getId());
                    node.put("op", e.getOperator());
                    node.put("min", e.getThresholdMin());
                    node.put("max", e.getThresholdMax());
                    arrayNode.add(node);
                }
                jedis.set(key,arrayNode.toString());
//                System.out.println("key:"+key+";value:"+arrayNode.toString());
            }
            jedis.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(conn!=null){
                try{
                    conn.close();
                }catch (Exception e){
                    log.error(e);
                }finally {
                    conn = null;
                }
            }
            if(prep!=null) {
                try {
                    prep.close();
                } catch (Exception e) {
                    log.error(e);
                } finally {
                    prep = null;
                }
            }
            if(jedis!=null) {
                try {
                    jedis.close();
                } catch (Exception e) {
                    log.error(e);
                } finally {
                    jedis = null;
                }
            }
        }
    }
}
